package com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.impl;

import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.HonestPtjobChildNativeRepository;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.repository.entity.HonestPtjobChildEntity;
import com.css.gdzj.gdzjbxgd.modules.reportinfo.viewobjects.HonestPtjobChildQueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;

/**
 * @author z
 * @date 2020-12-11
 */
@Repository
public class HonestPtjobChildNativeRepositoryImpl implements HonestPtjobChildNativeRepository {
    @PersistenceContext
    private EntityManager em;

    private static final Logger log = LoggerFactory.getLogger(HonestPtjobChildNativeRepositoryImpl.class);


    @Override
    public Page<HonestPtjobChildEntity> queryHonestPtjobChilds(HonestPtjobChildQueryParams queryParams, int curPage, int pageSize) {
        StringBuilder sql = new StringBuilder("select * from honest_ptjob_child ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!StringUtils.isEmpty(queryParams.getHonestPtjobUnit())) {
            whereSql.append(" and HONEST_PTJOB_UNIT like :honestPtjobUnit");
        }
        if (!StringUtils.isEmpty(queryParams.getHonestPtjobPost())) {
            whereSql.append(" and HONEST_PTJOB_POST like :honestPtjobPost");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HonestPtjobChildEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from honest_ptjob_child ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(queryParams.getHonestPtjobUnit())) {
            query.setParameter("honestPtjobUnit", "%" + queryParams.getHonestPtjobUnit() + "%");
            countQuery.setParameter("honestPtjobUnit", "%" + queryParams.getHonestPtjobUnit() + "%");
        }
        if (!StringUtils.isEmpty(queryParams.getHonestPtjobPost())) {
            query.setParameter("honestPtjobPost", "%" + queryParams.getHonestPtjobPost() + "%");
            countQuery.setParameter("honestPtjobPost", "%" + queryParams.getHonestPtjobPost() + "%");
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<HonestPtjobChildEntity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<HonestPtjobChildEntity> honestPtjobChildPage = new PageImpl<>(list, pageable, count.longValue());
        return honestPtjobChildPage;
    }


    @Override
    public List<HonestPtjobChildEntity> queryHonestPtjobChild(HonestPtjobChildQueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from honest_ptjob_child ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!StringUtils.isEmpty(queryParams.getHonestPtjobUnit())) {
            whereSql.append(" and HONEST_PTJOB_UNIT like :honestPtjobUnit");
        }
        if (!StringUtils.isEmpty(queryParams.getHonestPtjobPost())) {
            whereSql.append(" and HONEST_PTJOB_POST like :honestPtjobPost");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HonestPtjobChildEntity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from honest_ptjob_child ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(queryParams.getHonestPtjobUnit())) {
            query.setParameter("honestPtjobUnit", "%" + queryParams.getHonestPtjobUnit() + "%");
            countQuery.setParameter("honestPtjobUnit", "%" + queryParams.getHonestPtjobUnit() + "%");
        }
        if (!StringUtils.isEmpty(queryParams.getHonestPtjobPost())) {
            query.setParameter("honestPtjobPost", "%" + queryParams.getHonestPtjobPost() + "%");
            countQuery.setParameter("honestPtjobPost", "%" + queryParams.getHonestPtjobPost() + "%");
        }
        return query.getResultList();
    }

    @Override
    public List<HonestPtjobChildEntity> getList(String reportId) {
        StringBuilder sql = new StringBuilder("select * from honest_ptjob_child ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!StringUtils.isEmpty(reportId)) {
            whereSql.append(" and REPORT_ID = :reportId");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), HonestPtjobChildEntity.class);

        StringBuilder countSql = new StringBuilder("select count(*) from honest_ptjob_child ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!StringUtils.isEmpty(reportId)) {
            query.setParameter("reportId", reportId);
            countQuery.setParameter("reportId", reportId);
        }
        List<HonestPtjobChildEntity> list = query.getResultList();
//        Collections.reverse(list);
        return (null != list && list.size() > 0) ? list : null;
    }
}
